package com.itheima.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.domain.Product;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Repository
public interface ProductDao extends BaseMapper<Product> {

    @Select("SELECT COUNT(*) FROM product WHERE userId = #{userId} AND categoryId = #{categoryId} AND title=#{title} AND price = #{price} AND conditionLevel = #{conditionLevel} AND status = 1")
    int findSameProducts(@Param("userId") int userId,
                         @Param("categoryId") int categoryId,
                         @Param("title") String title,
                         @Param("price") BigDecimal price,
                         @Param("conditionLevel") int conditionLevel);

    @Select("SELECT * FROM product WHERE userId = #{userId} AND categoryId = #{categoryId} AND title=#{title} AND price = #{price} AND conditionLevel = #{conditionLevel} AND status = 1")
    Product findProduct(Product product);

    @Select("SELECT * FROM product WHERE status = 1 ORDER BY createTime DESC LIMIT #{limit}")
    List<Product> selectLatestProducts(@Param("limit") int limit);

    @Select("SELECT * FROM product WHERE productId = #{productId}")
    Product selectByProductId(@Param("productId") int productId);

    @Select("SELECT * FROM product WHERE status = 1 ORDER BY createTime DESC")
    List<Product> selectAllProducts();

    @Select("SELECT title From product WHERE productId = #{productId}")
    String getProductTitle(@Param("productId") int productId);

    @Select("SELECT imageUrl From product WHERE productId = #{productId}")
    String getProductImage(@Param("productId") int productId);

    @Select("SELECT price From product WHERE productId = #{productId}")
    BigDecimal getProductPrice(@Param("productId") int productId);

    @Select("SELECT description From product WHERE productId = #{productId}")
    String getProductDescription(@Param("productId") int productId);

    @Select("SELECT conditionLevel From product WHERE productId = #{productId}")
    int getProductConditionLevel(@Param("productId") int productId);

    @Select("select * from product where userId = #{userId}")
    List<Product> findProductsByUserId(@Param("userId") int userId);

    @Insert("INSERT INTO product (userId ,categoryId ,title ,description ,price ,originalPrice ,conditionLevel ,status ,createTime) " +
            "VALUES (#{userId} ,#{categoryId} ,#{title} ,#{description} ,#{price} ,#{originalPrice} ,#{conditionLevel} ,1 ,#{createTime})")
    int insertProduct(Product product);

    @Update("UPDATE product SET imageUrl = #{imageUrl} WHERE productId = #{productId}")
    void updateImage(@Param("imageUrl") String imageUrl, @Param("productId") int productId);

    //售出
    @Update("update product set status = -1 where productId = #{productId}")
    int soldProduct(@Param("productId") int productId);

    //下架
    @Update("update product set status = 0 where productId = #{productId}")
    int downProduct(@Param("productId") int productId);

    //上架
    @Update("update product set status = 1 where productId = #{productId}")
    int uploadProduct(@Param("productId") int productId);

    //删除记录，不再做显示
    @Update("update product set status = -2 where productId = #{productId}")
    int deleteProductEver(@Param("productId") int productId);

    //更新
    @Update("update product set title= #{title}, categoryId = ${categoryId}, price = #{price}, " +
            "originalPrice = #{originalPrice}, conditionLevel = #{conditionLevel}, description = #{description} " +
            "where productId = #{productId}")
    int updateProductInfo(Product product);
}
